package zhtt;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * Created by zhtt on 2017/4/10.
 */
public class StudentMainCurdTest {

    @Test
    public void save(){
        Student student=new Student("zhang san",20,20);
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            session.save(student);
            session.getTransaction().commit();
            System.out.println(student.getId());
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Test
    /** save为Hibernate的API、persist为JPA的API **/
    public void persist(){
        Student student=new Student("zhang san",20,20);
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            session.persist(student);
            session.getTransaction().commit();
            System.out.println(student.getId());
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Test
    public void delete(){
        Session session=HbnUtil.getSession();
        Student student=new Student(5);
        student.setName("a");//数据库中对应的非空字段必须set一个任意的值
        try{
            session.beginTransaction();
            session.delete(student);
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Test
    public void update(){
        Student student=new Student("wang san",20,20);
        student.setId(4);
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            session.update(student);
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Test
    //根据传入的ID自动判断保存或更新
    public void saveOrUpdate(){
        Student student=new Student("wang san",21,20);
        student.setId(4);
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            session.saveOrUpdate(student);
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Test
    //查询不到时返回null，不会抛异常
    public void get(){
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            Student student=session.get(Student.class,4);
            Student student2=session.get(Student.class,5);
            System.out.println(student);//打印：Student{id=4, name='wang san', age=21, score=20.0}
            System.out.println(student2);//打印：null
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Test
    //查询不到时抛出异常
    public void load(){
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            Student student=session.load(Student.class,5);
            System.out.println(student);
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    //对于增、删、改的底层sql语句执行顺序：insert、update、delete
    //若要改变顺序，则可以通过session.flush()完成
}

/**
 * 为了提高开发效率，创建一个工具类
 */
class HbnUtil{
    private static SessionFactory sessionFactory;

    public static Session getSession(){
        if(sessionFactory==null||sessionFactory.isClosed()){
            sessionFactory=new Configuration().configure().buildSessionFactory();
        }
        return sessionFactory.getCurrentSession();
    }
}
